package com.moriarty.socketim.service;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.widget.RemoteViews;
import android.widget.Toast;

import com.moriarty.socketim.IMApplication;
import com.moriarty.socketim.R;
import com.moriarty.socketim.activity.ChatActivity;
import com.moriarty.socketim.core.OnMessageListener;
import com.moriarty.socketim.core.QQConnection;
import com.moriarty.socketim.entity.BuddyInfo;
import com.moriarty.socketim.entity.QQMessage;
import com.moriarty.socketim.entity.QQMessageType;
import com.moriarty.socketim.util.ThreadUtil;

/**
 * Created by Moriarty on 16/11/18.
 */

public class MessageService extends Service {

    private static final int CHAT_NOTIFICATION = 0;

    private OnMessageListener listener = new OnMessageListener() {
        @Override
        public void onGetMessage(final QQMessage msg) {
            ThreadUtil.runOnUIThread(new Runnable() {
                @Override
                public void run() {
                    if (QQMessageType.MSG_TYPE_CHAT_P2P.equals(msg.type)) {
                        BuddyInfo buddyInfo = new BuddyInfo();
                        buddyInfo.setNick(msg.fromNick);
                        buddyInfo.setAvatar(msg.fromAvatar);
                        buddyInfo.setAccount(msg.from);
                        //换成notification
                        showNotification(buddyInfo);

                    }
                }
            });
        }
    };

    //显示通知
    public void showNotification(BuddyInfo buddyInfo) {
        //设置参数
        Notification.Builder builder = new Notification.Builder(getApplicationContext());
        builder.setSmallIcon(R.drawable.ic_launcher);
        builder.setTicker("chat with :" + buddyInfo.getNick());
        builder.setWhen(System.currentTimeMillis());
        //其实也没啥用...主要是5.0以后的smallIcon需要是背景透明的白色图标 不然就是全白的....非常尴尬
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            builder.setColor(Color.RED);
        }
        builder.setContent(getRemoteView(buddyInfo));//设置通知的布局文件
        //弹出
        Notification notification = builder.getNotification();

        NotificationManager notificationService = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationService.notify(CHAT_NOTIFICATION, notification);
    }

    private RemoteViews getRemoteView(BuddyInfo buddyInfo) {
        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.chat_notify);
        //设置textView的显示内容
        remoteViews.setTextViewText(R.id.tv_buddyname_notify, buddyInfo.getNick());
        remoteViews.setTextViewText(R.id.tv_chatcontent_notify, String.valueOf(buddyInfo.getAccount()));

        // 设置点击事件
        remoteViews.setOnClickPendingIntent(R.id.ll_notify, getContentIntent(buddyInfo));

        return remoteViews;

    }

    //获取pendingIntent
    private PendingIntent getContentIntent(BuddyInfo buddyInfo) {
        Intent intent = new Intent(this, ChatActivity.class);//class应该是activity
        intent.putExtra("buddyInfo", buddyInfo);
        //这里应该是getActivity
        PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        return pendingIntent;
    }

    //取消通知
    public void cancelNotification() {
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.cancel(CHAT_NOTIFICATION);
    }

    IMApplication imApplication;
    private QQConnection mQqConnection;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        imApplication = (IMApplication) getApplication();
        mQqConnection = imApplication.getQqConnection();
        mQqConnection.addOnMessageListener(listener);

    }

    @Override
    public void onDestroy() {
        mQqConnection.removeOnMessageListener(listener);
        super.onDestroy();
    }
}
